/*!
 @header AccountInfoViewController.m
 @author 周益
 @version 1.0.0 2013/02/22 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "AccountInfoViewController.h"
#import "Helper.h"
#import "Config.h"
#import "IntegralRecordsViewController.h"
#import "CashRecordsViewController.h"
#import "ProductsRecordsViewController.h"
#import "RechargeViewController.h"
#import "TransferViewController.h"
#import "StatisticViewController.h"


//  -业务逻辑细节说明-
//
//  @abstract       AccountInfoViewController功能较为简单，视图上半部分为一个imageview，下半部分为一个tableview。
//                     上半部分显示账户余额和积分，显示充值和转账按钮；下半部分提供查看销售记录、购买记录、充值记录、转账记录和积分记录。

//  @discussion     在viewWillAppear执行时，VC会向服务器请求用户的余额和积分信息，然后显示。
//
//                      点击充值按钮会present充值VC，进入到充值流程中；点击转账按钮会present转账VC，进入到转账流程中。
//                      点击tableview中的任意一行，即可到对应记录VC中。

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本
//  修改描述


@interface AccountInfoViewController ()

@end

@implementation AccountInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    //账户信息背景
    UIView *infoBackgroudView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 110.0f)];
    infoBackgroudView.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_bg_001@2x" ofType:@"png"]]];
    
    [self.view addSubview:infoBackgroudView];
    
    //账户余额
    _accountCashLabel = [[RTLabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 100.0f, 60.0f)];
    _accountCashLabel.font = _SYS_FONT_004;
    _accountCashLabel.textAlignment = RTTextAlignmentCenter;
    _accountCashLabel.text = [NSString stringWithFormat:_L(@"tpl_09"),0.00f];
    
    [infoBackgroudView addSubview:_accountCashLabel];
    
    //账户积分
    _accountIntegralLabel = [[RTLabel alloc] initWithFrame:CGRectMake(320.0f - 100.0f - 25.0f, 10.0f, 100.0f, 60.0f)];
    _accountIntegralLabel.font = _SYS_FONT_004;
    _accountIntegralLabel.textAlignment = RTTextAlignmentCenter;
    _accountIntegralLabel.text = [NSString stringWithFormat:_L(@"tpl_10"),0];
    
    [infoBackgroudView addSubview:_accountIntegralLabel];
    
    //充值按钮
    UIButton *rechargeButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 70.0f, 140.0f, 30.0f)];
    [rechargeButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_008_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    [rechargeButton setTitle:_L(@"btn_yes_12") forState:UIControlStateNormal];    //充值
    [rechargeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rechargeButton.titleLabel.font = _SYS_FONT_005;
    [rechargeButton addTarget:self action:@selector(rechargeButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [infoBackgroudView addSubview:rechargeButton];
    
    //转账按钮 
    UIButton *transferButton = [[UIButton alloc] initWithFrame:CGRectMake(170.0f, 70.0f, 140.0f, 30.0f)];
    [transferButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_006_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    [transferButton setTitle:_L(@"btn_yes_13") forState:UIControlStateNormal];    //转账
    [transferButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    transferButton.titleLabel.font = _SYS_FONT_005;
    [transferButton addTarget:self action:@selector(transferButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [infoBackgroudView addSubview:transferButton];
    
    //tableview
    UITableView *accountTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 130.0f, 320.0f, _SYS_SCREEN_HEIGHT - 44.0f - 20.0f - 130.0f) style:UITableViewStyleGrouped];
    
    if (iOS7) {
        accountTableView.frame = CGRectMake(0.0f, 110.0f, 320.0f, _SYS_SCREEN_HEIGHT - 24.0f - 20.0f - 130.0f);
    }
    accountTableView.backgroundColor = _SYS_COLOR_BACKGROUND;
    accountTableView.delegate = self;
    accountTableView.dataSource = self;
    
    accountTableView.backgroundView = nil;
    
    //设置不可滚动
    accountTableView.scrollEnabled = NO;
    
    [self.view addSubview:accountTableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self accountRequest];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        UILabel *recordLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 24.0f)];
        
        recordLabel.font = _SYS_FONT_007;
        recordLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:recordLabel];
    
    }
    
    //获得label
    UILabel *recordLabel = [[cell.contentView subviews] objectAtIndex:0];
    
    //设置cell中label的值
    switch (indexPath.row) {
        case AccountInfoSaleRecords:
            recordLabel.text = _L(@"vc_title_040");   //销售记录
            break;
        case AccountInfoPurchaseRecords:
            recordLabel.text = _L(@"vc_title_041");   //购买记录
            break;
        case AccountInfoRechargeRecords:
            recordLabel.text = _L(@"vc_title_042");   //充值记录
            break;
        case AccountInfoTransferRecords:
            recordLabel.text = _L(@"vc_title_043");   //转账记录
            break;
        case AccountInfoIntegralRecords:
            recordLabel.text = _L(@"vc_title_044");   //积分记录
            break;
        case AccountInfoRecommendRecords:
            recordLabel.text = _L(@"vc_title_113");   //积分记录
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case AccountInfoSaleRecords:
        {
            ProductsRecordsViewController *productRecordsVC = [[ProductsRecordsViewController alloc] initWithIsSaleRecordType:YES];
            
            [productRecordsVC.navigationItem setTitle:_L(@"vc_title_040")];   //销售记录
            [self.navigationController pushViewController:productRecordsVC animated:YES];
            
        }
            break;
        case AccountInfoPurchaseRecords:
        {
            ProductsRecordsViewController *productRecordsVC = [[ProductsRecordsViewController alloc] initWithIsSaleRecordType:NO];
            
            [productRecordsVC.navigationItem setTitle:_L(@"vc_title_041")];   //购买记录
            [self.navigationController pushViewController:productRecordsVC animated:YES];
        }
            break;
        case AccountInfoRechargeRecords:
        {
            CashRecordsViewController *cashRecordsVC = [[CashRecordsViewController alloc] initWithRecordsType:NO];
            
            [cashRecordsVC.navigationItem setTitle:_L(@"vc_title_042")];   //充值记录
            [self.navigationController pushViewController:cashRecordsVC animated:YES];
        }
            
            break;
        case AccountInfoTransferRecords:
        {
            CashRecordsViewController *cashRecordsVC = [[CashRecordsViewController alloc] initWithRecordsType:YES];
            
            [cashRecordsVC.navigationItem setTitle:_L(@"vc_title_043")];   //提现记录
            [self.navigationController pushViewController:cashRecordsVC animated:YES];
        }
            break;
        case AccountInfoIntegralRecords:
        {
            IntegralRecordsViewController *integralRecordsVC = [[IntegralRecordsViewController alloc] init];
            
            [integralRecordsVC.navigationItem setTitle:_L(@"vc_title_044")];   //积分记录
            [self.navigationController pushViewController:integralRecordsVC animated:YES];
        }
            break;
        case AccountInfoRecommendRecords:
        {
            StatisticViewController *statisticVC = [[StatisticViewController alloc] init];
            [statisticVC.navigationItem setTitle:_L(@"vc_title_113")];
            [self.navigationController pushViewController:statisticVC animated:YES];
        }
            break;
        default:
            break;
    }
}


#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*!
 @abstract 充值按钮事件
 */
- (void)rechargeButtonEvent:(id)sender
{
    
    //充值
    RechargeViewController *rechargeVC = [[RechargeViewController alloc] init];    
//    UINavigationController *rechargeNC = [[UINavigationController alloc] initWithRootViewController:rechargeVC];
//    [Helper navigationControllerCommonStyle:rechargeNC];
//    [self.navigationController presentModalViewController:rechargeNC animated:YES];
    [self.navigationController pushViewController:rechargeVC animated:true];
}

/*!
 @abstract 提现按钮事件
 */
- (void)transferButtonEvent:(id)sender
{
    //转账
    TransferViewController *transferVC = [[TransferViewController alloc] init];
    
    [transferVC.navigationItem setTitle:_L(@"btn_yes_13")];
    
    UINavigationController *transferNC = [[UINavigationController alloc] initWithRootViewController:transferVC];
    [Helper navigationControllerCommonStyle:transferNC];
    [self.navigationController presentModalViewController:transferNC animated:YES];
    
}

#pragma mark - 私有方法

/*!
 @abstract 向服务器请求个人账户信息
 */
- (void)accountRequest
{
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_GET_ACCOUNT];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");
    [request asynchronous];
}


#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        NSDictionary *accountInfo = [dict objectForKey:@"data"];
        
        //设置余额和积分
        _accountCashLabel.text = [NSString stringWithFormat:_L(@"tpl_09"), [[accountInfo objectForKey:@"balance"] floatValue]];
        _accountIntegralLabel.text = [NSString stringWithFormat:_L(@"tpl_10"), [[accountInfo objectForKey:@"validScore"] integerValue]];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }

}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}
@end
